<?php

namespace Renshan\DetectorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Renshan\Http\Request as CRequest;
use RenshanHttp\Response as CResponse;

class IndexController extends Controller
{
    /**
     *
     * 创建一个探测的Action
     * 这里只是数据中转和返回
     * 真正的处理实在Renshan\DetectorBundle\Service\Target的create方法
     *
     */
    public function createAction(Request $request)
    {
        $params = $request->get('params');
        $params = json_decode($params, true);

        $title      = $params['title'];
        $url        = $params['url'];
        $method     = $params['method'];
        $headers    = $params['headers'];
        $timing     = $params['timing'];
        $parameters = $params['params'];

        $target_service = $this->get('renshan_detector.target');

        $target     = $target_service->create($title, $url, $method, $timing, $headers, $parameters);

        $params['id'] = $target->getId();

        $ret_array  = ['code' => 1, 'data' => $params];

        $response   = new Response;
        $response->headers->set('Content-Type', 'application/json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");
        $response->setContent(json_encode($ret_array));

        return $response;
    }

    public function getAction(Request $request)
    {
        $page       = $request->get('page');
        $page_size  = $request->get('page_size');


        $ret = $this->get('renshan_detector.target')->pagination($page, $page_size);

        $response = new Response();
        $response->headers->set('Content-Type', 'application/json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");
        $response->setContent(json_encode($ret));

        return $response;
    }

    public function copyAction(Request $request)
    {
        $id = $request->get('id');

        $ret = $this->get('renshan_detector.target')->copy($id, $this->getUser());

        $response = new Response();
        $response->headers->set('Content-Type', 'application/json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");
        $response->setContent(json_encode($ret));

        return $response;
    }

    public function  delAction(Request $request)
    {
        $id = $request->get('id');

        $ret = $this->get('renshan_detector.target')->delete($id);

        $response = new Response();
        $response->headers->set('Content-Type', 'application/json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");
        $response->setContent(json_encode($ret));

        return $response;
    }

    public function updateAction(Request $request)
    {
        $params = $request->get('params');
        $params = json_decode($params, true);

        $id         = $params['id'];
        $title      = $params['title'];
        $url        = $params['url'];
        $method     = $params['method'];
        $headers    = $params['headers'];
        $timing     = $params['timing'];
        $parameters = $params['params'];

        $target_service = $this->get('renshan_detector.target');

        $ret_array     = $target_service->update($id, $title, $url, $method, $timing, $headers, $parameters);

        $response   = new Response;
        $response->headers->set('Content-Type', 'application/json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");
        $response->setContent(json_encode($ret_array));

        return $response;
    }

    public function detectAction(Request $request)
    {
        $id = $request->get('id');

        $ret_array = $this->get('renshan_detector.target')->detect($id, $this->getUser());

        $response   = new Response;
        $response->headers->set('Content-Type', 'application/json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");
        $response->setContent(json_encode($ret_array));

        return $response;
    }

    public function getAllAction()
    {

        $target_service = $this->get('renshan_detector.target');

        $targets = $target_service->getAll($this->getUser());

        $ret_array = ['code' => 1, 'data' => $targets];

        $response = new Response(json_encode($ret_array));
        $response->headers->set('Content-Type', 'Application/Json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");

        return $response;
    }

    public function createGroupAction(Request $request)
    {
        $group_service = $this->get('renshan_detector.group');

        $ret_array = $group_service->create($this->getUser());

        $response = new Response(json_encode($ret_array));
        $response->headers->set('Content-Type', 'Application/Json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");

        return $response;
    }

    public function getGroupAction(Request $request)
    {
        $page       = $request->get('page');
        $page_size  = $request->get('page_size');

        $group_service = $this->get('renshan_detector.group');

        $ret_array = $group_service->pagination($page, $page_size);

        $response = new Response(json_encode($ret_array));
        $response->headers->set('Content-Type', 'Application/Json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");

        return $response;
    }

    public function delGroupAction(Request $request)
    {
        $id = $request->get('id');

        $group_service = $this->get('renshan_detector.group');

        $ret_array = $group_service->delete($id);

        $response = new Response(json_encode($ret_array));
        $response->headers->set('Content-Type', 'Application/Json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");

        return $response;
    }

    public function updateGroupAction()
    {
        $ret_array = $this->get('renshan_detector.group')->update();

        $response = new Response(json_encode($ret_array));
        $response->headers->set('Content-Type', 'Application/Json');
        $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:3000');
        $response->headers->set('Access-Control-Allow-Credentials', "true");

        return $response;
    }

    public function createUserAction(Request $request)
    {
        $ret = ['code' => -1];

        $response = new Response;
        $response->headers->set('Content-Type', 'Application/Json');


        $params = $request->get('params');
        $params = json_decode($params, true);

        if (json_last_error() != 0) {
            return $response->setContent(json_encode($ret));
        }

        $userManager = $this->get('fos_user.user_manager');

        $username   = $params['username'];
        $password   = $params['password'];
        $email      = $params['email'];

        if ($username == null || $password == null || $email == null) {
            $ret['message'] = '信息不全';
            return $response->setContent(json_encode($ret));
        }

        $user       = $userManager->createUser();

        if ($userManager->findUserByUsername($username)) {
            $ret['message'] = '帐号已存在';
            return $response->setContent(json_encode($ret));
        }

        if ($userManager->findUserByEmail($email)) {
            $ret['message'] = 'Email已存在';
            return $response->setContent(json_encode($ret));
        }

        $user->setUsername($username);
        $user->setPlainPassword($password);
        $user->setEmail($email);
        $user->setEnabled(true);

        $em = $this->get('doctrine')->getManager();
        $em->persist($user);
        $em->flush();

        $ret['code'] = 1;

        return $response->setContent(json_encode($ret));
    }


}
